package com.wondertek.mist.plugins;

import java.util.Arrays;
import java.util.List;

import org.apache.cordova.api.CallbackContext;
import org.apache.cordova.api.CordovaPlugin;
import org.apache.cordova.api.PluginResult;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.baidu.android.pushservice.PushManager;
import com.eternity.util.Utils;

public class BaiduPushPlugin extends CordovaPlugin{

	public static boolean webviewReady=false;
	
	@Override
    public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
		if("setTags".equalsIgnoreCase(action)){
			//args = [ tag ]
			String tag=args.getString(0);
			List<String> tags = Arrays.asList(tag.split(","));
			PushManager.setTags(this.cordova.getActivity(), tags);
			callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK));
		}else if("getPushInfo".equalsIgnoreCase(action)){
			JSONObject result = new JSONObject();
			result.put("userid", Utils.getString(this.cordova.getActivity(), "push.user_id"));
			result.put("channelid", Utils.getString(this.cordova.getActivity(), "push.channel_id"));
			
			callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK,result));
		}else if("webviewReady".equalsIgnoreCase(action)){
			BaiduPushPlugin.webviewReady=true;
			callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK));
		}
		return true;
	}
}
